﻿var isValid = true;
var msgBox = null;
var t = null;
var data = [];
$(function () {

    //初始化选中
    var trs = $("#tbList").find('tr');
    for (var i = 2; i < trs.length; i++) {
        if ($($($("#tbList").find('tr').get(i)).find('td').get(5)).text().indexOf("是") >= 0)
        {
            var strTr =$($("#tbList").find('tr').get(i)).find('td');
            $($("#tbList").find('tr').get(i)).find('input').attr('checked', 'checked');
            $("input[name=txtName]").val($(strTr.get(1)).text());
            $("input[name=txtAddress]").val($(strTr.get(2)).text());
            $("input[name=txtPhone]").val($(strTr.get(3)).text());
            $("input[name=txtPostCode]").val($(strTr.get(4)).text());
        }
    }
    $("#btnGoPay").click(PayMoney);
    $('input[id=btndialogOk]').bind('click', function () {
        if (doAddress()) {
            $("#_typeId").val("1");
            $('#_dialogModify').dialog('close');
        }      
    });
    $('input[id=btndialogCancel]').bind('click', function () {
         $("#_typeId").val("");
         $('#_dialogModify').dialog('close');
    });
    //$("#_dialogModify").dialog({
    //    autoOpen: false,
    //    width: 500,
    //    modal: true,
    //    buttons: {
    //        //修改面板的 确定按钮 方法，提交 新数据到 服务器，并处理返回值
    //        "确定": function () {
    //            if (doAddress())
    //            {
    //                $("#_typeId").val("1");
    //                //2.关闭当前窗体
    //                $(this).dialog("close");
    //            }
    //        },
    //        "取消": function () {
    //            $("#_typeId").val("");
    //            $(this).dialog("close");
    //        }
    //    }
    //});
    $('input[value=checkbox]').bind('change', function () {
        debugger;
        var s = $(this);
        var trs = $("#tbList").find('tr');
        for (var i = 2; i < trs.length; i++) {
            if ($($("#tbList").find('tr').get(i)).find('input').attr('name') != s.attr('name')) {
                $($("#tbList").find('tr').get(i)).find('input').removeAttr('checked');
            } else
            {
                $("input[name=txtName]").val($(s.parent().parent().find('td').get(1)).text());
                $("input[name=txtAddress]").val($(s.parent().parent().find('td').get(2)).text());
                $("input[name=txtPhone]").val($(s.parent().parent().find('td').get(3)).text());
                $("input[name=txtPostCode]").val($(s.parent().parent().find('td').get(4)).text());
            }
        }
    });
    $('span[class=orderState]').click(ManOrder);
    $('input[id=btnAddress]').click(showAddress);
    $('a[title=新增地址]').click(showAddress);
    $("a[EditId=EditId]").click(CreateViewHtml);
    $("#PrvId").bind("change", function () {
        var strShengId = "";
        var prvId = $("#PrvId").val();
        for (var i = 0; i < data.length;i++){
        if (data[i].sonId == prvId) {
            strShengId += '<option value=' + data[i].fatherId + ' title=' + data[i].name + '>' + data[i].name + '</option>';
          }
        }
        $("#ShengId").html("").html(strShengId);
    });
});
function showAddress()
{
    $("#txtName").val("");
    $("#txtAddress").val("");
    $("#txtPhone").val("");
    $.post("../Action/orderComfirm.ashx", {type:"area"}, function (jsObj)
    {
        data = jsObj.data;
        debugger;
        var strOps = "";
        var strShengId = "";
        for (var i = 0; i < jsObj.data.length; i++)
        {           
            if (jsObj.data[i].sonId == 0)
            {
                strOps += '<option value=' + jsObj.data[i].fatherId + ' title=' + jsObj.data[i].name + '>' + jsObj.data[i].name + '</option>';
            }
        }
        $("#PrvId").html("").html(strOps);
        for (var j = 0; j < jsObj.data.length; j++){
            if (jsObj.data[j].sonId==jsObj.data[1].fatherId) {
                strShengId += '<option value=' + jsObj.data[j].fatherId + ' title=' + jsObj.data[j].name + '>' + jsObj.data[j].name + '</option>';
            }
        }
        $("#ShengId").html("").html(strShengId);
    }, "json");
    $("#txtPostCode").val("");
    $("#_dialogModify").dialog("open");
}
function CreateViewHtml() {
    debugger;
    var bookId = $(this).attr("id");
    $.post("../Action/BookList.ashx", { type: "Edit", bookId: bookId }, function (jsObj) {
        //processData(jsObj);
        if (jsObj.data) {
            window.open(jsObj.data);
        }
    }, "json");
}
function getMoRenAddress(id,btn)
{
        t = $(btn);
        //$("input[name='chk_"+id+"']:checkbox").attr('checked', 'checked');
        $.post("../Action/orderComfirm.ashx", { type: "mraddress", id: id }, function (jsObj) {
            debugger;
            if (jsObj.statu == "ok") {
                if (jsObj.data.statu == true) {
                    var trs = $("#tbList").find('tr');
                    for (var i = 2; i < trs.length; i++) {
                        if ($($("#tbList").find('tr').get(i)).find('input').attr('name') == "chk_" + jsObj.data.Id + "") {
                            $("input[name='chk_" + jsObj.data.Id + "']:checkbox").attr('checked', 'checked');
                            $(t).parent().html("").html('是<a href="javascript:void(0)"  title="设置" onclick="getMoRenAddress(' + jsObj.data.Id + ',this)" id=' + jsObj.data.Id + ' style="cursor:pointer; margin-left:5px;">设置</a>');
                        } else {
                            var id = $($("#tbList").find('tr').get(i)).find('input').attr('name').replace("chk_","");
                            $($("#tbList").find('tr').get(i)).find('input').removeAttr('checked');
                            $($($("#tbList").find('tr').get(i)).find('td').get(5)).html("").append('否<a href="javascript:void(0)" onclick="getMoRenAddress(' + id + ',this)" title="设置" id=' + id + ' style="cursor:pointer; margin-left:5px;">设置</a>');
                        }
                    }
                }

            }
        }, "json")
}
function doAddress()
{
    if ($("#txtName").val().trim() == "") {
        msgBox.showMsgErr("姓名不能为空~~！");
        return false;
    } else if ($("#txtAddress").val().trim() == "") {
        msgBox.showMsgErr("详细地址不能为空~~！");
        return false;
    } else if ($("#txtPhone").val().trim() == "") {
        msgBox.showMsgErr("手机不能为空~~！");
        return false;
    } else if ($("#txtPostCode").val().trim() == "") {
        msgBox.showMsgErr("邮编不能为空~~！");
        return false;
    } else if (checkMobile($("#txtPhone").val().trim()) == false) {
        msgBox.showMsgErr("手机格式有误~~！");
        return false;
    } else if (checkpostCode($("#txtPostCode").val().trim()) == false) {
        msgBox.showMsgErr("邮编格式有误~~！");
        return false;
    }
    $("input[name=txtName]").val($("#txtName").val());
    var address = $($("#PrvId").find('option')[$("#PrvId").get(0).selectedIndex]).attr('title') + "省" +
                  $($("#ShengId").find('option')[$("#ShengId").get(0).selectedIndex]).attr('title') + $("#txtAddress").val();
    $("input[name=txtAddress]").val(address);
    $("input[name=txtPhone]").val($("#txtPhone").val());
    $("input[name=txtPostCode]").val($("#txtPostCode").val());
    $.post("../Action/orderComfirm.ashx", {
        type: "address", name: $("#txtName").val(), address: address, phone: $("#txtPhone").val(),
        postCode: $("#txtPostCode").val()}, function (jsObj)
    {
            if ($("#tbList").find('tr').length == 0) {
                var table = '<table id="tbList" style="width:90%;"><tr id="trNav"><th colspan="6">用户【' + $("#useraddId").val() + '】的收货地址</th></tr><tr><th>操作</th><th>收货人</th><th>地址</th><th>电话</th><th>邮编</th><th>是否为默认地址</th></tr><tr><td><input type="checkbox" name="chk_'+jsObj.data.Id+'" value="checkbox" style="width:10%;"/></td><td style="width:10%">' + jsObj.data.UserName + '</td><td style="width:35%">' + jsObj.data.Address + '</td><td style="width:25%">' + jsObj.data.TelPhone + '</td><td style="width:10%">' + jsObj.data.postCode + '</td>';
                var IsEdit = "";
                if (jsObj.data.statu == false)
                {
                    IsEdit = "否";
                }
                else 
                {
                    IsEdit = "是";
                }
                var table1 = '<td style="width:10%">' + IsEdit + '<a href="javascript:void(0)"  title="设置" onclick="getMoRenAddress(' + jsObj.data.Id + ',this)" id=' + jsObj.data.Id + ' style="cursor:pointer; margin-left:5px;">设置</a></td></tr></table>';
                $("#divTable").html("").html(table+table1);
            } else
            {
                var table2 = '<tr><td><input type="checkbox" name="chk_' + jsObj.data.Id + '" value="checkbox" style="width:10%;"/></td><td style="width:10%">' + jsObj.data.UserName + '</td><td style="width:35%">' + jsObj.data.Address + '</td><td style="width:25%">' + jsObj.data.TelPhone + '</td><td style="width:10%">' + jsObj.data.postCode + '</td>';
                var IsEdit = "";
                if (jsObj.data.statu == false) {
                    IsEdit = "否";
                }
                else {
                    IsEdit = "是";
                }
                var table3 = '<td style="width:10%">' + IsEdit + '<a href="javascript:void(0)"  title="设置" onclick="getMoRenAddress(' + jsObj.data.Id + ',this)" id=' + jsObj.data.Id + ' style="cursor:pointer; margin-left:5px;">设置</a></td></tr>';
                $("#tbList").append(table2 + table3);
            }
        }, "json")
    return true;
}
function ManOrder() {
    if ($(this).text() == "已处理") {
        msgBox.showMsgErr("此订单已被处理~~！");
        return;
    }
    if (window.confirm('是否处理订单？')) {
        t=$(this);
        var orderId = $(this).attr("orderId");
        $.post("../Action/OrderManager.ashx", { type: "order", orderId: orderId }, function (jsObj) {
            debugger;
            if (jsObj.data == true)
            {
                $(t).text("已处理");
            }
            processData(jsObj);
            debugger;
        }, "json");
    } else {
        return ;
    }
}
function PayMoney() {
    Validate();
    if (isValid == false) {
        msgBox.showMsgErr("请检查收货信息是否填写完整~~！");
        //$("#spanMsg").text("请检查收货信息是否填写完整!");
        return false;
    }
    if ($("#_typeId").val() == "")
    {
        msgBox.showMsgErr("请填写收货信息~~！");
        return;
    }   
    $("#hdType").val("1");
   var name= $("input[name=txtName]").val();
   var address = $("input[name=txtAddress]").val();
   var phone = $("input[name=txtPhone]").val();
   var postCode = $("input[name=txtPostCode]").val();
   $.post("../Action/orderComfirm.ashx", { type: "paymoney", name: name, address: address, phone: phone, postCode: postCode }, function (jsObj) {
       debugger;
       processData(jsObj);
       debugger;
    }, "json");
}
function Validate() {
    isValid = true;
    $("#userinfo input[type=text]").each(
           function () {

               if ($(this).val().length <= 0) {
                   isValid = false;
                   $($(this).next()).attr("style", "display:inline");
               }

           }

        );
}
function OnSubmit() {
    Validate();
    if (isValid == false) {
        msgBox.showMsgErr("请检查收货信息是否填写完整~~！");
        //$("#spanMsg").text("请检查收货信息是否填写完整!");
        return false;
    }
    $("#hdType").val("1");

}
function processData(jsObj, okFunc, errFunc) {
    //根据返回的 数据 状态 执行相应的操作
    switch (jsObj.statu) {
        case "ok"://如果ok则执行 ok回调函数
            if (okFunc) okFunc();
            msgBox.showMsgOk(jsObj.msg);
            break;
        case "err"://如果err的话，则 执行 err 回调函数
            if (errFunc) errFunc();
            msgBox.showMsgErr(jsObj.msg);
            break;
        case "np"://没有权限，则直接跳转到指定页面
            msgBox.showMsgErr(jsObj.msg, function () {
                window.location = jsObj.nextUrl;
            });
            break;
    }
}